package kr.hkit.score_pjt.UI;

import java.awt.BorderLayout;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import kr.hkit.score_pjt.DTO.StdInfo;

public class StdinfoListUI extends JFrame {

	private JPanel contentPane;
	private ArrayList<StdInfo> lists;
	private JTable table;
	CustomDataModel model;
	
	public StdinfoListUI(ArrayList<StdInfo> lists) {
		this.lists = lists;
		initialize();
	}
	private void initialize() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		
		JLabel lblNewLabel = new JLabel("성적 현황");
		panel.add(lblNewLabel);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		model = new CustomDataModel();
		table = new JTable(model);
		scrollPane.setViewportView(table);
		alignData();
	}
	
	public void refreshLitst(ArrayList<StdInfo> lists){
		this.lists = lists;
		model.fireTableDataChanged();
		alignData();
	}
	
	public void alignData(){
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		
		for(int i=0; i<4; i++){
			table.getColumnModel().getColumn(i).setCellRenderer(dtcrCenter);
		}
		
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		for(int i=4; i<9; i++){
			table.getColumnModel().getColumn(i).setCellRenderer(dtcrRight);
		}
	}
	
	class CustomDataModel extends AbstractTableModel{
		final String[] ColumnName ={"월급","순위","학번","성명","국어","영어","수학","총점","평균"};
		@Override
		public int getRowCount() {
			return lists.size();
		}

		@Override
		public int getColumnCount() {
			return ColumnName.length;
		}

		@Override
		public String getColumnName(int column) {
			return ColumnName[column];
		}

		@Override
		public Object getValueAt(int row, int col) {
			StdInfo std = lists.get(row);
			switch(col){
			case 0: return std.getStdGrade();
			case 1: return std.getStdRank();
			case 2: return std.getStdNum();
			case 3: return std.getStdName();
			case 4: return std.getKorScore();
			case 5: return std.getEngScore();
			case 6: return std.getMathScore();
			case 7: return std.getTotal();
			case 8: return std.getAvg();
			}
			return null;
		}
		
	}

}
